package com.example.lambda.date;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

public class Test {

   // 多线程会报错 ,线程不安全
    @org.junit.Test
    public void test() throws ExecutionException, InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Callable<Date> call = new Callable<Date>() {
            @Override
            public Date call() throws Exception {
                return sdf.parse("20210516");
            }
        };
        ExecutorService pool = Executors.newFixedThreadPool(10);
        List<Future<Date>> list = new ArrayList<>();
        for (int i =0 ;i<10;i++){
            list.add(pool.submit(call));
        }
        for (Future<Date> future:list){
            System.out.println(future.get());
        }
    }

    // 线程安全

    @org.junit.Test
    public void test2() throws ExecutionException, InterruptedException {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");

        Callable<LocalDate> call = new Callable<LocalDate>() {
            @Override
            public LocalDate call() throws Exception {
                return LocalDate.parse("20210516");
            }
        };
        ExecutorService pool = Executors.newFixedThreadPool(10);
        List<Future<LocalDate>> list = new ArrayList<>();
        for (int i =0 ;i<10;i++){
            list.add(pool.submit(call));
        }
        for (Future<LocalDate> future:list){
            System.out.println(future.get());
        }
    }
}
